/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.system.service.impl;

import me.zhengjie.modules.system.domain.CameraMonitor;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.system.repository.CameraMonitorRepository;
import me.zhengjie.modules.system.service.CameraMonitorService;
import me.zhengjie.modules.system.service.dto.CameraMonitorDto;
import me.zhengjie.modules.system.service.dto.CameraMonitorQueryCriteria;
import me.zhengjie.modules.system.service.mapstruct.CameraMonitorMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://docs.auauz.net
* @description 服务实现
* @author 黄星星
* @date 2020-05-24
**/
@Service
@RequiredArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class CameraMonitorServiceImpl implements CameraMonitorService {

    private final CameraMonitorRepository cameraMonitorRepository;
    private final CameraMonitorMapper cameraMonitorMapper;

    @Override
    public Map<String,Object> queryAll(CameraMonitorQueryCriteria criteria, Pageable pageable){
        Page<CameraMonitor> page = cameraMonitorRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(cameraMonitorMapper::toDto));
    }

    @Override
    public List<CameraMonitorDto> queryAll(CameraMonitorQueryCriteria criteria){
        return cameraMonitorMapper.toDto(cameraMonitorRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    public CameraMonitorDto findById(Long id) {
        CameraMonitor cameraMonitor = cameraMonitorRepository.findById(id).orElseGet(CameraMonitor::new);
        ValidationUtil.isNull(cameraMonitor.getId(),"CameraMonitor","id",id);
        return cameraMonitorMapper.toDto(cameraMonitor);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public CameraMonitorDto create(CameraMonitor resources) {
        resources.setCid(0l);

        return cameraMonitorMapper.toDto(cameraMonitorRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(CameraMonitor resources) {
        CameraMonitor cameraMonitor = cameraMonitorRepository.findById(resources.getId()).orElseGet(CameraMonitor::new);
        ValidationUtil.isNull( cameraMonitor.getId(),"CameraMonitor","id",resources.getId());
        cameraMonitor.copy(resources);
        cameraMonitorRepository.save(cameraMonitor);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long id : ids) {
            cameraMonitorRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<CameraMonitorDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (CameraMonitorDto cameraMonitor : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("创建时间", cameraMonitor.getCreateTime());
            map.put("更新时间", cameraMonitor.getUpdateTime());
            map.put("创建者", cameraMonitor.getCreateBy());
            map.put("更新者", cameraMonitor.getUpdateBy());
            map.put("监控名称", cameraMonitor.getName());
            map.put("平台地址", cameraMonitor.getPlatUrl());
            map.put("平台token", cameraMonitor.getPlatToken());
            map.put("码流", cameraMonitor.getCodeFlow());
            map.put("监控地址", cameraMonitor.getMonitorUrl());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}